package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.dto.LoginDto;
import com.heima.admin.entity.AdUser;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2021-12-22
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 新增自媒体用户
     *
     * @param user
     * @return
     */
    @Override
    @Transactional
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        //1.根据APP用户ID查询自媒体用户是否存在
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        //2.存在直接返回自媒体用户
        queryWrapper.eq(WmUser::getApUserId, user.getApUserId());
        WmUser one = this.getOne(queryWrapper);
        if (one != null) {
            return ResponseResult.okResult(one);
        }
        //3.反之保存后返回
        this.save(user);
        //int i = 1 / 0;
        return ResponseResult.okResult(user);
    }

    /**
     * 自媒体用户登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //1.校验入参必填项
        if (dto == null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.根据账户查询自媒体用户信息
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getName, dto.getName());
        WmUser one = this.getOne(queryWrapper);
        if (one == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //3.校验用户填写密码是否正确
        //3.1 将入参中用户填写密码+盐进行MD5加密 得到 密文
        String pwdStr = dto.getPassword() + one.getSalt();
        String userEnterPwdEncodedStr = DigestUtils.md5DigestAsHex(pwdStr.getBytes());
        //3.2 从用户记录中获取正确密码 跟 上一步得到密文进行比较
        if (!one.getPassword().equals(userEnterPwdEncodedStr)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //4.认证通过后，基于JWT生成用户令牌,载荷中需要两个属性 userId userName
        Map<String, Object> claimMaps = new HashMap<>();
        claimMaps.put("userId", one.getId());
        claimMaps.put("userName", one.getName());
        String token = AppJwtUtil.getToken(claimMaps);

        //5.封装响应数据（user,token） 其中用户信息中敏感数据要做脱敏处理
        //5.1 对返回用户信息做脱敏处理
        one.setSalt(null);
        one.setPhone(null);
        one.setPassword(null);
        //5.2 响应结果对象
        HashMap<String, Object> mapResult = new HashMap<>();
        mapResult.put("user", one);
        mapResult.put("token", token);
        return ResponseResult.okResult(mapResult);
    }

   /* public static void main(String[] args) {
        String pwd = "admin"+"adminsalt";
        String s = DigestUtils.md5DigestAsHex(pwd.getBytes());
        System.out.println(s);
    }*/
}
